#!/bin/bash

BASE_ANN_DIR=/home/mixagol/data/6_annotation/20120802/annotation_cluster_done


# Functions
for d in ${BASE_ANN_DIR}/NC_*; do echo $d; done | xargs -n1 -P1 9_annotation_analysis/create_results_go.sh 'full' 'entry'
for d in ${BASE_ANN_DIR}/NC_*; do echo $d; done | xargs -n1 -P1 9_annotation_analysis/create_results_go.sh 'full' 'exact'
for d in ${BASE_ANN_DIR}/NC_*; do echo $d; done | xargs -n1 -P1 9_annotation_analysis/create_results_go.sh 'types' 'entry'
for d in ${BASE_ANN_DIR}/NC_*; do echo $d; done | xargs -n1 -P1 9_annotation_analysis/create_results_go.sh 'types' 'exact'

# Pathways
for d in ${BASE_ANN_DIR}/NC_*; do echo $d; done | xargs -n1 -P1 9_annotation_analysis/create_results_pathways_coverage.sh 'full' 'entry'
for d in ${BASE_ANN_DIR}/NC_*; do echo $d; done | xargs -n1 -P1 9_annotation_analysis/create_results_pathways_coverage.sh 'full' 'exact'
for d in ${BASE_ANN_DIR}/NC_*; do echo $d; done | xargs -n1 -P1 9_annotation_analysis/create_results_pathways_coverage.sh 'types' 'entry'
for d in ${BASE_ANN_DIR}/NC_*; do echo $d; done | xargs -n1 -P1 9_annotation_analysis/create_results_pathways_coverage.sh 'types' 'exact'

# Backup
find $BASE_ANN_DIR/ -maxdepth 2 -type f -name 'results_*' \
    | sed 's#'"$BASE_ANN_DIR"'/##g' \
    | tar \
        -czf $BASE_ANN_DIR/../annotation_cluster_results_backup_$(date +%s).tar.gz \
        -C $BASE_ANN_DIR \
        --files-from -

# GO FULL aggr statistic
for d in $BASE_ANN_DIR/NC_* ; do
    echo -en $(basename $d)"\t"
    cat $d/results_go_full_exact.txt \
        | cut -f3 \
        | awk '{print $1}'     \
        | awk '{if ($1>10) {print 0} else print $1}'     \
        | sort \
        | uniq -c \
        | 9_annotation_analysis/analize_annotation.py --tsv-all
done \
    | awk '{print $2"\t"$4"\t"$10"\t"$22"\t"$25"\t"$28}' \
    | awk '{AL+=$1; AN+=$2; ANPR+=$3; ANPR5+=$4; ANPR10+=$5; NOANPR+=$6}END{print AL"\t"AN/AL"\t"ANPR/AL"\t"ANPR5/AL"\t"ANPR10/AL"\t"NOANPR/AL}'


# GO TYPES aggr statistic
for d in $BASE_ANN_DIR/NC_* ; do
    echo -en $(basename $d)"\t"
    cat $d/results_go_types_exact.txt \
        | cut -f3 \
        | awk '{print $1" "$2" "$3}' \
        | sed 's/.*P:\([-0-9.]\+\).*/\1/g'     \
        | awk '{if ($1>10) {print 0} else print $1}'     \
        | sort \
        | uniq -c \
        | 9_annotation_analysis/analize_annotation.py --tsv-all
done \
    | awk '{print $2"\t"$4"\t"$10"\t"$22"\t"$25"\t"$28}' \
    | awk '{AL+=$1; AN+=$2; ANPR+=$3; ANPR5+=$4; ANPR10+=$5; NOANPR+=$6}END{print AL"\t"AN/AL"\t"ANPR/AL"\t"ANPR5/AL"\t"ANPR10/AL"\t"NOANPR/AL}'



# PW FULL aggr statistic
for d in $BASE_ANN_DIR/NC_* ; do
    echo -en $(basename $d)"\t"
    cat $d/results_pw_coverage_full_exact.txt \
        | cut -f3- \
        | 9_annotation_analysis/analize_pw_coverage.py \
        | awk '{if ($1<0) {I=$1} else {I=0; C[$1]++; D[$1]+=$2; D2[$1]+=$3}; {S[I]++}} \
               END{ printf("%4i %4i %4i %4i", S[-3], S[-2], S[-1], S[0]); \
                    for (i=1; i<=30; i++) \
                       {if (C[i]>0) {F=D[i]/C[i]; F2=D2[i]/C[i]} else {F=0; F2=0}; printf("\t%2i: %3i %.3f %.3f", i, C[i], F, F2)}; \
                    printf("\n") \
               }'
done  \
    | awk -F'\t' \
        '{n=split($2, MS, " "); S[-3]+=MS[1]; S[-2]+=MS[2]; S[-1]+=MS[3]; S[0]+=MS[4]; \
            for (j=3; j<=NF; j++) { split($j, MI, ":"); i=int(MI[1]/5)*5; split(MI[2], MF, " "); C[i]+=MF[1]; D[i]+=MF[2]*MF[1]; D2[i]+=MF[3]*MF[1] }} \
         END{ printf("%6i %6i %6i %6i", S[-3], S[-2], S[-1], S[0]); \
             for (i=0; i<=30; i++) \
                 {if (C[i]>0) {F=D[i]/C[i]; F2=D2[i]/C[i];  printf("\t%2i: %3i %.3f %.3f", i, C[i], F, F2)}}; \
             printf("\n") \
         }'



# PW TYPES aggr statistic ===OLD!!!===
for d in $BASE_ANN_DIR/NC_* ; do
#   echo -en $(basename $d)"\t"
    cat $d/results_pw_coverage_types_exact.txt \
        | cut -f3- \
        | 9_annotation_analysis/analize_pw_coverage.py --type P \
        | awk '{if ($1<0) {I=$1} else {I=0; C[$1]++; D[$1]+=$2; D2[$1]+=$3}; {S[I]++}} \
                END{ for (var in S) {print var"\t"S[var]}; for (var in C) {print var"\t"C[var]"\t"D[var]/C[var]"\t"D2[var]/C[var]}}'
done \
    | awk '{if ($1<=0) {S[$1]+=$2} else {C[$1]+=$2; D[$1]+=$3*$2; D2[$1]+=$4*$2};} \
            END{ for (var in S) {print var"\t"S[var]}; for (var in C) {print var"\t"C[var]"\t"D[var]/C[var]"\t"D2[var]/C[var]}}' \
    | sort -k1,1 -n -t$'\t'



##
## GRP RESULTS
##

# GO FULL aggr statistic
for compare_func in "exact" "entry"; do

for fname in 8_other_systems/genomes_grp*; do
    fn=$(basename $fname)
    method=$(cat $fname | cut -f2 | uniq)
    #echo -ne $method"\t"
    echo -ne $fn"\t"

for d in `cat $fname | cut -f1` ; do
    echo -en $(basename $d)"\t"
    cat $BASE_ANN_DIR/$d/results_go_full_${compare_func}.txt \
        | cut -f3 \
        | awk '{print $1}'     \
        | awk '{if ($1>10) {print 0} else print $1}'     \
        | sort \
        | uniq -c \
        | 9_annotation_analysis/analize_annotation.py --tsv-all
done \
    | awk '{print $2"\t"$4"\t"$10"\t"$22"\t"$25"\t"$28}' \
    | awk '{AL+=$1; AN+=$2; ANPR+=$3; ANPR5+=$4; ANPR10+=$5; NOANPR+=$6} \
          END{ printf("%6i\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n", AL, AN/AL, ANPR/AL, ANPR5/AL, ANPR10/AL, NOANPR/AL) }'

done > 9_annotation_analysis/results_go_full_${compare_func}.txt

done


# GO TYPES aggr statistic
for compare_func in "exact" "entry"; do
for type in "P" "F" "C"; do

for fname in 8_other_systems/genomes_grp*; do
    fn=$(basename $fname)
    method=$(cat $fname | cut -f2 | uniq)
    #echo -ne $method"\t"
    echo -ne $fn"\t"

for d in `cat $fname | cut -f1` ; do
    echo -en $(basename $d)"\t"
    cat $BASE_ANN_DIR/$d/results_go_types_${compare_func}.txt \
        | cut -f3 \
        | awk '{print $1" "$2" "$3}' \
        | sed 's/.*'"${type}"':\([-0-9.]\+\).*/\1/g'     \
        | awk '{if ($1>10) {print 0} else print $1}'     \
        | sort \
        | uniq -c \
        | 9_annotation_analysis/analize_annotation.py --tsv-all
done \
    | awk '{print $2"\t"$4"\t"$10"\t"$22"\t"$25"\t"$28}' \
    | awk '{AL+=$1; AN+=$2; ANPR+=$3; ANPR5+=$4; ANPR10+=$5; NOANPR+=$6} \
          END{ printf("%6i\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n", AL, AN/AL, ANPR/AL, ANPR5/AL, ANPR10/AL, NOANPR/AL) }'

done > 9_annotation_analysis/results_go_types_${compare_func}_${type}.txt

done
done




# PW FULL aggr statistic
for compare_func in "exact" "entry"; do

for fname in 8_other_systems/genomes_grp*; do
    fn=$(basename $fname)
    method=$(cat $fname | cut -f2 | uniq)
    #echo -ne $method"\t"
    echo -ne $fn"\t"

for d in `cat $fname | cut -f1` ; do
    echo -en $(basename $d)"\t"
    cat $BASE_ANN_DIR/$d/results_pw_coverage_full_${compare_func}.txt \
        | cut -f3- \
        | 9_annotation_analysis/analize_pw_coverage.py \
        | awk '{if ($1<0) {I=$1} else {I=0; C[$1]++; D[$1]+=$2; D2[$1]+=$3}; {S[I]++}} \
               END{ printf("%4i %4i %4i %4i", S[-3], S[-2], S[-1], S[0]); \
                    for (i=1; i<=30; i++) \
                       {if (C[i]>0) {F=D[i]/C[i]; F2=D2[i]/C[i]} else {F=0; F2=0}; printf("\t%2i: %3i %.3f %.3f", i, C[i], F, F2)}; \
                    printf("\n") \
               }'
done  \
    | awk -F'\t' \
        '{n=split($2, MS, " "); S[-3]+=MS[1]; S[-2]+=MS[2]; S[-1]+=MS[3]; S[0]+=MS[4]; \
            for (j=3; j<=NF; j++) { split($j, MI, ":"); i=int(MI[1]/5)*5; split(MI[2], MF, " "); C[i]+=MF[1]; D[i]+=MF[2]*MF[1]; D2[i]+=MF[3]*MF[1] }} \
         END{ printf("%6i %6i %6i %6i", S[-3], S[-2], S[-1], S[0]); \
             for (i=0; i<=30; i++) \
                 {if (C[i]>0) {F=D[i]/C[i]; F2=D2[i]/C[i];  printf("\t%2i: %5i %.3f %.3f", i, C[i], F, F2)}}; \
             printf("\n") \
         }'

done > 9_annotation_analysis/results_pw_full_${compare_func}.txt

done




# PW TYPES aggr statistic
for compare_func in "exact" "entry"; do
for type in "P" "F" "C"; do

for fname in 8_other_systems/genomes_grp*; do
    fn=$(basename $fname)
    method=$(cat $fname | cut -f2 | uniq)
    #echo -ne $method"\t"
    echo -ne $fn"\t"

for d in `cat $fname | cut -f1` ; do
    echo -en $(basename $d)"\t"
    cat $BASE_ANN_DIR/$d/results_pw_coverage_types_${compare_func}.txt \
        | cut -f3- \
        | 9_annotation_analysis/analize_pw_coverage.py --type ${type} \
        | awk '{if ($1<0) {I=$1} else {I=0; C[$1]++; D[$1]+=$2; D2[$1]+=$3}; {S[I]++}} \
               END{ printf("%4i %4i %4i %4i", S[-3], S[-2], S[-1], S[0]); \
                    for (i=1; i<=30; i++) \
                       {if (C[i]>0) {F=D[i]/C[i]; F2=D2[i]/C[i]} else {F=0; F2=0}; printf("\t%2i: %3i %.3f %.3f", i, C[i], F, F2)}; \
                    printf("\n") \
               }'
done  \
    | awk -F'\t' \
        '{n=split($2, MS, " "); S[-3]+=MS[1]; S[-2]+=MS[2]; S[-1]+=MS[3]; S[0]+=MS[4]; \
            for (j=3; j<=NF; j++) { split($j, MI, ":"); i=int(MI[1]/5)*5; split(MI[2], MF, " "); C[i]+=MF[1]; D[i]+=MF[2]*MF[1]; D2[i]+=MF[3]*MF[1] }} \
         END{ printf("%6i %6i %6i %6i", S[-3], S[-2], S[-1], S[0]); \
             for (i=0; i<=30; i++) \
                 {if (C[i]>0) {F=D[i]/C[i]; F2=D2[i]/C[i];  printf("\t%2i: %5i %.3f %.3f", i, C[i], F, F2)}}; \
             printf("\n") \
         }'

done > 9_annotation_analysis/results_pw_types_${compare_func}_${type}.txt

done
done



